package com.example.statelayouthelper

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import cbfg.state.LayoutHandler
import cbfg.state.StateLayoutHelper
import com.example.statelayouthelper.databinding.FragmentDemo22Binding

/**
 * @Description: LayoutHandler 测试
 * @Author: TomHawk
 * @CreateDate: 2022/8/18 17:02
 *
 * 说明：使用 LayoutHandler 可以根据需要来将状态布局添加到容器中
 */
class Demo22Fragment : Fragment(R.layout.fragment_demo_22) {
    private var _binding: FragmentDemo22Binding? = null

    private val binding: FragmentDemo22Binding
        get() = _binding!!

    private lateinit var statusLayoutHelper: StateLayoutHelper

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = FragmentDemo22Binding.bind(view)
        binding.btnTest1.setOnClickListener {
            initStatusLayout()
            statusLayoutHelper.showState(STATE_LOADING)
            Toast.makeText(view.context, "挡住我看小说啦！！！", Toast.LENGTH_SHORT).show()
        }
        binding.btnTest2.setOnClickListener {
            initStatusLayout2()
            statusLayoutHelper.showState(STATE_LOADING)
        }
        binding.tvTxt.movementMethod = ScrollingMovementMethod.getInstance()
    }

    private fun initStatusLayout() {
        if (this::statusLayoutHelper.isInitialized) {
            statusLayoutHelper.clearStates()
        }
        statusLayoutHelper = StateLayoutHelper.Builder(binding.root)
            .addState(STATE_LOADING, STATE_LOADING)
            .setStateClickListener { helper, state, view ->
                if (state == STATE_LOADING && view.id == R.id.btnStateCancel) {
                    helper.hideState(STATE_LOADING)
                }
            }
            .build()
    }

    private fun initStatusLayout2() {
        if (this::statusLayoutHelper.isInitialized) {
            statusLayoutHelper.clearStates()
        }

        statusLayoutHelper = StateLayoutHelper.Builder(binding.root)
            .addState(STATE_LOADING, STATE_LOADING)
            .config(object : LayoutHandler {
                override fun handle(container: ViewGroup, state: Int, stateView: View): Boolean {
                    if (state == STATE_LOADING) {
                        val layoutParams = ConstraintLayout.LayoutParams(200, 200)
                        layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                        layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
                        layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
                        layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
                        container.addView(stateView, layoutParams)
                        return true
                    }
                    return false
                }
            })
            .setStateClickListener { helper, state, view ->
                if (state == STATE_LOADING && view.id == R.id.btnStateCancel) {
                    helper.hideState(STATE_LOADING)
                }
            }
            .build()
    }

    override fun onDestroy() {
        _binding = null
        super.onDestroy()
    }

    companion object {
        private const val STATE_LOADING = R.layout.layout_state_loading2
    }
}